package com.bjpowernode.controller;

import com.bjpowernode.pojo.ProductInfo;
import com.bjpowernode.pojo.vo.ProductInfoVo;
import com.bjpowernode.service.ProductInfoService;
import com.bjpowernode.utils.FileNameUtil;
import com.github.pagehelper.PageInfo;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/prod")
public class ProductInfoAction {
    // 每页显示的记录数
    public static final int PAGE_SIZE = 5;

    // 异步上传的图片名称
    String saveFileName = "";

    // 切记!! 所有的界面层一定有业务逻辑层的对象
    @Autowired
    ProductInfoService productInfoService;

    // 显示所有商品 【不分页】
    @RequestMapping("/getAll")
    public String getAll(HttpServletRequest request) {
        List<ProductInfo> list = productInfoService.getAll();
        request.setAttribute("list", list);
        return "product";
    }

    // 显示第一页五条记录
    @RequestMapping("/split")
    public String split(HttpServletRequest request) {
        PageInfo info = null;
        Object vo = request.getSession().getAttribute("prodVo");
        if (vo != null) {
            info = productInfoService.splitPageVo((ProductInfoVo) vo, PAGE_SIZE);
            request.getSession().removeAttribute("prodVo");
        } else {
            // 得到第一页的数据
            info = productInfoService.splitPage(1, PAGE_SIZE);
        }
        request.setAttribute("info", info);
        return "product";
    }

    // 【使用ajax翻页处理，局部刷新】
    @ResponseBody
    @RequestMapping("/ajaxSplit")
    public void ajaxSplit(ProductInfoVo vo, HttpSession session) {
        // 取得当前page参数的页面数据
        PageInfo info = productInfoService.splitPageVo(vo, PAGE_SIZE);
        session.setAttribute("info", info);
    }

    // 异步ajax文件上传处理
    @ResponseBody
    @RequestMapping("/ajaxImg")
    public Object ajaxImg(MultipartFile pimage, HttpServletRequest request) {
        // 提取生成文件名UUID+上传图片的后缀
        String saveFileName = FileNameUtil.getUUIDFileName() + FileNameUtil.getFileType(pimage.getOriginalFilename());
        // 得到项目中图片的存储路径
        String path = request.getServletContext().getRealPath("/image_big");
        // saveFileName 就是类似这样的【40ddfad6e9ef49feb32e1840cea0fb4c.jpg】文件名
        // 转存
        try {
            pimage.transferTo(new File(path + File.separator + saveFileName));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 返回客户端JSON对象，封装图片的路径，为了在页面实现立即回显
        JSONObject object = new JSONObject();
        object.put("imgurl", saveFileName);
        return object.toString();
    }

    @RequestMapping("/save")
    public String save(ProductInfo info, HttpServletRequest request) {
        info.setpImage(saveFileName);
        info.setpDate(new Date());
        // info对象中有表单提交上来的5个数据，有异步ajax上传的图片名称数据，有上传时间的数据
        int num = -1;
        try {
            num = productInfoService.save(info);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (num > 0) {
            request.setAttribute("msg", "添加成功!");
        } else {
            request.setAttribute("msg", "添加失败!");
        }
        // 清空saveFileName变量中的内容，为了下次增加或修改的异步ajax的上传处理
        saveFileName = "";
        // 添加成功后应该重新访问数据库，所以跳转到分页显示的action上
        return "forward:/prod/split.action";
    }

    @RequestMapping("/one")
    public String one(int pid, ProductInfoVo vo, Model model, HttpSession session) {
        ProductInfo info = productInfoService.getById(pid);
        model.addAttribute("prod", info);
        // 将多条件及页码放入session中，更新处理结束后读取条件和页码进行再处理
        session.setAttribute("prodVo", vo);
        return "update";
    }

    @RequestMapping("/update")
    public String update(ProductInfo info, HttpServletRequest request) {
        // ajax异步图片上传，如果有上传过则saveFileName里有图片名称
        //如果没有上传过，则saveFileName为空,实体类info使用隐藏表单提供上来的pImage原始图片的名称
        if (!saveFileName.equals("")) {
            info.setpImage(saveFileName);
        }
        // 完成更新处理
        int num = -1;
        try {
            num = productInfoService.update(info);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (num > 0) {
            // 更新成功
            request.setAttribute("msg", "更新成功");
        } else {
            // 更新失败
            request.setAttribute("msg", "更新失败");
        }
        // 依然清空saveFileName变量
        saveFileName = "";
        return "forward:/prod/split.action";
    }

    @RequestMapping("/delete")
    public String delete(int pid, ProductInfoVo vo, HttpServletRequest request) {
        // 单个商品删除
        int num = -1;
        try {
            num = productInfoService.delete(pid);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (num > 0) {
            request.setAttribute("msg", "删除成功");
            request.getSession().setAttribute("deleteProdVo", vo);
        } else {
            request.setAttribute("msg", "删除失败 ");
        }
        // 删除结束后跳转到分页显示
        return "forward:/prod/deleteAjaxSplit.action";
    }

    @ResponseBody
    @RequestMapping(value = "/deleteAjaxSplit", produces = "text/html;charset=UTF-8")
    public Object deleteAjaxSplit(HttpServletRequest request) {
        // 取得第一页的数据
        PageInfo info =null;
        Object vo = request.getSession().getAttribute("deleteProdVo");
        if (vo!=null){
            info=productInfoService.splitPageVo((ProductInfoVo) vo,PAGE_SIZE);
        } else {
            info=productInfoService.splitPage(1,PAGE_SIZE);
        }
        request.getSession().setAttribute("info", info);
        return request.getAttribute("msg");
    }

    // 批量删除商品
    @RequestMapping("/deleteBatch")
    public String deleteBatch(String pids, HttpServletRequest request) {
        // 将上传上来的字符串截开，形成商品id的字符数组
        String[] ps = pids.split(",");
        try {
            int num = productInfoService.deleteBatch(ps);
            if (num > 0) {
                request.setAttribute("msg", "批量删除成功!");
            } else {
                request.setAttribute("msg", "批量删除失败!");
            }
        } catch (Exception e) {
            request.setAttribute("msg", "商品不可删除!");
        }
        return "forward:/prod/deleteAjaxSplit.action";
    }

    // 多条件查询功能实现
    @ResponseBody
    @RequestMapping("/condition")
    public void condition(ProductInfoVo vo, HttpSession session) {
        List<ProductInfo> list = productInfoService.selectCondition(vo);
        session.setAttribute("list", list);
    }

}
